<template>
    <div>
      <div class="card card-custom gutter-b example example-compact" id="tableBody">
        <div class="card-header">
          <div class="card-title">
            <span class="card-icon">              
              <i class="text-dark-50 flaticon-search-magnifier-interface-symbol"></i>
            </span>
            <h3 class="card-label"> 查询区域 </h3>
          </div>
          <div class="card-toolbar">
            <div class="example-tools justify-content-center">
              <!-- 
                <span data-toggle="tooltip" class="example-toggle"></span>
                <span data-toggle="tooltip" class="example-copy"></span> 
              -->
            </div>
          </div>
        </div>
        <div class="card-body">
            <!--查询条件-->
            <div class="m-form m-form--fit m-form--label-align-left m-form--group-seperator-dashed">
                <div class="m-portlet__body">
                    <div class="form-group m-form__group row">
                        <label class="col-form-label">授权系统:</label>
                        <div class="col-lg-3">
                            <el-select v-model="searchForm.sysModeId" placeholder="请选择">
                                <el-option value="">请选择</el-option>
                                <el-option
                                v-for="item in sysModeList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                                </el-option>
                            </el-select>
                        </div>
                        <label class="col-form-label">类型:</label>
                        <div class="col-lg-3">
                          <el-select maxlength="20" v-model="searchForm.type" placeholder="请选择">
                              <el-option value="">请选择</el-option>
                              <el-option
                                  v-for="item in [{value:'0',label:'平台权限'},{value:'1',label:'业务权限'}]"
                                  :key="item.value"
                                  :label="item.label"
                                  :value="item.value">
                              </el-option>
                          </el-select>
                        </div>
                        <label class="col-form-label">角色名称:</label>
                        <div class="col-lg-2">
                            <input type="text" class="form-control" v-model="searchForm.name" placeholder="请输入">
                        </div>
                        
                    </div>
                    <div class="form-group m-form__group row">
                      <label class="col-form-label">角色编码:</label>
                      <div class="col-lg-2">
                          <input type="text" class="form-control" v-model="searchForm.code" placeholder="请输入">
                      </div>
                      <b-button :pressed="false" variant="btn btn-primary font-weight-bold font-size-sm py-3 px-8 mr-2 " @click="search()"><span><i
                      class="flaticon-search-magnifier-interface-symbol"></i><span>查 询</span></span></b-button>
                      <b-button :pressed="false" variant="btn btn-light font-weight-bold font-size-sm py-3 px-8 mr-2 " @click="resetAll()">
                      <span><i class="flaticon2-refresh"></i><span>重 置</span></span></b-button> 
                      <button class="btn btn-light-primary font-weight-bold font-size-sm py-3 px-8 mr-2 "
                      v-permission="{ action: '/oauthRole/add', effect: 'disabled' }"
                      @click="addOauthRole">
                          <span><i class="la la-plus la-lg"></i><span>新 建</span></span>
                      </button>
                      <button class="btn btn-secondary m-btn m-btn--custom m-btn--label-info m-btn--bolder font-size-sm py-3 px-8 mr-2"
                      v-permission="{ action: '/oauthRole/recover', effect: 'disabled' }"  
                      @click="initListDeleted">
                        <span><i class="fa fa-history fa-lg"></i><span>找回角色</span></span>
                      </button>                        
                      <button class="btn btn-light-success m-btn m-btn--custom m-btn--label-info m-btn--bolder font-size-sm py-3 px-8 mr-2"
                      v-permission="{ action: '/oauthRole/export', effect: 'disabled' }"    
                      @click="exportRole">
                        <span><i class="fas fa-file-export"></i><span>导出</span></span>
                      </button>
                    </div>
                </div>
                <div class="m-portlet__foot m-portlet__no-border m-portlet__foot--fit">
                    <div class="m-form__actions m-form__actions--solid">
                        <div class="row">
                            <div class="col m--align-left">
                            </div>
                            <div class="col m--align-right">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--分页组件-->
        <el-table style="width: 100%" stripe border @selection-change="handleSelectionChange" highlight-current-row :data="roleList" >
          <template slot="empty">
            <el-empty description="暂无数据"></el-empty>
          </template>
          <el-table-column label="序号" min-width="50">
            <template slot-scope="scope">
              {{scope.$index+1}} 
            </template>
          </el-table-column>
          <el-table-column align="left" prop="name" show-overflow-tooltip label="角色名称"></el-table-column>
          <el-table-column align="left" prop="sysName" show-overflow-tooltip label="授权系统"></el-table-column>   
          <el-table-column align="left" prop="type" show-overflow-tooltip label="类型">
            <template slot-scope="scope">
              <div v-if="scope.row.type == 0">
                <span class="label label-lg label-light-primary label-inline mt-lg-0 mb-lg-0 my-2 font-weight-bold py-4">平台权限</span>
              </div>
              <div v-else-if="scope.row.type == 1">
                <span class="label label-lg label-light-warning label-inline mt-lg-0 mb-lg-0 my-2 font-weight-bold py-4">业务权限</span>
              </div>
              <div v-else>
                <span class="label label-lg label-light-warning label-inline mt-lg-0 mb-lg-0 my-2 font-weight-bold py-4">未知</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="left" prop="code" show-overflow-tooltip label="角色编码"></el-table-column>  
          <!-- <el-table-column align="left" prop="createBy" show-overflow-tooltip label="创建人"></el-table-column>   -->
          <el-table-column align="left" prop="createTime" show-overflow-tooltip label="创建时间"></el-table-column>  
          <!-- <el-table-column align="left" prop="modifiedBy" show-overflow-tooltip label="修改人"></el-table-column>  
          <el-table-column align="left" prop="updateTime" show-overflow-tooltip label="修改时间"></el-table-column>   -->
          <el-table-column align="center" label="操作" fixed="left" min-width="360">
            <template slot-scope="scope">
              <!--scope.row当前行的对象-->
              <button @click="getOauthRoleDetail(scope.row)" class="btn btn-link" title="详情">详 情</button>
              <button class="btn btn-link"
                  @click="updateOauthRole(scope.row)" 
                  v-permission="{ action: '/oauthRole/update', effect: 'disabled' }">
                  <span><i class="la la-pen-alt"></i><span>编 辑</span></span>
              </button>
              <button class="btn btn-link-danger"
              v-permission="{ action: '/oauthRole/delete', effect: 'disabled' }"    
              @click="delOauthRole(scope.row)">
                  <span><i class="far fa-trash-alt"></i><span>禁 用</span></span>
              </button>
              <button @click="addOauthResources(scope.row)" 
                v-permission="{ action: '/oauthRole/saveOauthRR', effect: 'disabled' }"
                class="btn btn-link" title="分配资源">
                <span><i class="la la-barcode"></i><span>分配资源</span></span>
              </button>
              <button 
              @click="addOauthUserRole(scope.row)" class="btn btn-link" title="分配账户">
                <span><i class="la la-user-cog"></i><span>分配账户</span></span>
              </button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            hide-on-single-page   
            @size-change="handleSizeChange"        
            @current-change="handleCurrentChange"  
            :current-page="pageNo"                
            :page-sizes="[5, 10, 30, 50]"          
            :page-size="pageSize"			  
            layout="->,total, sizes, prev, pager, next, jumper"
            :total="totalCount">		
          </el-pagination>
        </div>
      </div>
      <OauthRoleAdd ref="oauthRoleAddRef" @change="search"></OauthRoleAdd><!--采用父窗口关闭传递的方法-->
      <OauthRoleUpdate ref="oauthRoleUpdateRef" @change="search"></OauthRoleUpdate><!--采用父窗口关闭传递的方法-->
      <OauthRoleDetail ref="oauthRoleDetailRef"></OauthRoleDetail>
      <OauthRoleDelete ref="oauthRoleDeleteRef"></OauthRoleDelete>   
      <OauthUserRole ref="oauthUserRoleRef"></OauthUserRole>   
      <OauthRoleResources ref="oauthRoleResourcesRef"></OauthRoleResources>    
  </div> 
</template>
  
<script>
  import { SET_BREADCRUMB } from "@/store/breadcrumbs.module";
  import OauthRoleAdd from "@/view/oauth/oauth-role/oauth-role-add.vue";
  import OauthRoleUpdate from "@/view/oauth/oauth-role/oauth-role-update.vue";
  import OauthRoleDetail from "@/view/oauth/oauth-role/oauth-role-detail.vue";
  import OauthRoleDelete from "@/view/oauth/oauth-role/oauth-role-delete-list.vue";
  import OauthUserRole from "@/view/oauth/oauth-role/oauth-user-role.vue";
  import OauthRoleResources from "@/view/oauth/oauth-role/oauth-role-resources.vue";
  import Swal from "sweetalert2";
  import apiUtil from "@/core/util/apiUtil.js"; 
  import { handleNotify,handleAlert,handleConfirm,showWating,closeWating,downloadFileCallFn} from "@/core/util/jehcUtil.js";
  export default {
    data(){
      return{
        searchForm:{sysModeId: "",type:'',name:"",code:""},
        sysModeList:[],
        roleList:[],
        sels: [], //当前选框选中的值
        pageNo:1,      // 默认当前是第一页
        pageSize:5,    // 当前每页的数据是5条
        totalCount:0   // 总数默认为0
      }
    },
    components: {
        OauthRoleAdd,
        OauthRoleUpdate,
        OauthRoleDetail,
        OauthRoleDelete,
        OauthUserRole,
        OauthRoleResources
    },
    mounted() {
        this.$store.dispatch(SET_BREADCRUMB, [{ title: "角色权限" }]);
        this.initList();   // 按当前的页号和每页的数据量进行查询
        this.initSysModeList();
    },
    methods:{
      initList(){
        showWating({renderBody:"tableBody"});
        var params = {};
        params.usePageNo = true;//采用第几页进行分页（兼容）
        params.pageSize = this.pageSize;//页面显示记录条数，在页面显示每页显示多少项的时候
        params.pageNo = this.pageNo;//开始的记录序号   
        params.searchJson = JSON.stringify(this.searchForm);//为form元素     
        apiUtil.queryPage(process.env.VUE_APP_OAUTH_API+"/oauthRole/list?delFlag=0", params).then(({ data }) => {
          this.roleList = data.data;//给结果集赋值
          this.totalCount = data.total;// 获取当前数据的总数    
        });
      },         
      handleSelectionChange(sels) {//获取选中的值
        this.sels = sels;
      },
      addOauthRole(){//新建
        this.$refs.oauthRoleAddRef.showModal()
      },
      updateOauthRole(row){// 更新
        this.$refs.oauthRoleUpdateRef.showModal(row.id);
      },
      delOauthRole(row){ // 禁用
        // 禁用前提示
        this.$confirm("确认禁用记录吗?", "提示", {type: "warning",}).then(() => {
          apiUtil.delete(process.env.VUE_APP_OAUTH_API+"/oauthRole/delete?id="+row.id).then(data=>{
            if(data.data.success){
              handleAlert("禁用成功", "success", 3);
              this.search();
            }else {
              handleAlert("禁用失败", "error", 3);
            }
          });
        }).catch(()=>{});//注意这里，这里是重点！！！;        
      },
      handleSizeChange(val) { // 修改每页所存数据量的值所触发的函数
        this.pageSize = val;  // 修改页的大小
        this.initList();       // 按新的pageNo和pageSize进行查询
      },
      handleCurrentChange(val) { // 修改当前页所触发的函数
        this.pageNo = val;       // 更新当前的页
        this.initList();          // 按新的pageNo和pageSize进行查询
      },
      search(){
        this.pageNo = 1;       // 更新当前的页
        this.initList();          // 按新的pageNo和pageSize进行查询
      },
      resetAll(){
        Object.assign(this.$data.searchForm, this.$options.data().searchForm);
        this.search();
      },
      getOauthRoleDetail(row){
        this.$refs.oauthRoleDetailRef.showModal(row.id);
      },
      initSysModeList(){
        let params = {};
            apiUtil.query(process.env.VUE_APP_OAUTH_API+"/oauthSysMode/listAll", params).then(({ data }) => {
                this.sysModeList = data.data;
            });
      },
      initListDeleted(){
        this.$refs.oauthRoleDeleteRef.showModal();
      },
      addOauthUserRole(row){
        this.$refs.oauthUserRoleRef.showModal(row);
      },
      addOauthResources(row){
        this.$refs.oauthRoleResourcesRef.showModal(row);
      },
      exportRole(){//导出
        this.$confirm("确定导出角色?", "提示", {type: "warning",}).then(() => {
              let url = process.env.VUE_APP_OAUTH_API+'/oauthRole/export'; // 请求接口
              let method = "GET"; // 请求方式 GET或POST
              let fileName = "角色.xls"; // 下载后的文件名称
              var params = "name='test" // 后台接口需要的参数
              downloadFileCallFn(url, method, fileName,params);
          }).catch(()=>{});//注意这里，这里是重点！！！;  
      },
    }
  };
</script>